import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22652
 * Date: 2024-05-16
 * Time: 17:34
 */
public class SignForm extends JFrame {
    public static UserForm SignuserForm;
    public SignForm(UserForm userForm) throws HeadlessException {
        SignuserForm=userForm;

        setTitle("用户登录");
        setSize(400, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        setLocationRelativeTo(null);

        // 用户名输入
        JLabel usernameLabel = new JLabel("用户名:");
        JTextField usernameField = new JTextField(28);
        add(usernameLabel);
        add(usernameField);

        // 密码输入
        JLabel passwordLabel = new JLabel("密码:");
        JPasswordField passwordField = new JPasswordField(28);
        add(passwordLabel);
        add(passwordField);
        //确认密码
        JLabel passwordLabel1 = new JLabel("密码:");
        JPasswordField passwordField1 = new JPasswordField(28);
        add(passwordLabel1);
        add(passwordField1);
        //注册按钮
        JButton SignButton = new JButton("注册");
        add(SignButton);
        //返回按钮
        JButton RetButton = new JButton("返回");
        add(RetButton);

        SignButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());
                System.out.println(password);
                String password1 = new String(passwordField1.getPassword());
                System.out.println(password1);

                if (!UserForm.isUsername(username)) {
                    if (!password1.equals(password)){
                        JOptionPane.showMessageDialog(null, "两次密码不同", "错误", JOptionPane.ERROR_MESSAGE);
                    }else {
                        JOptionPane.showMessageDialog(null, "注册成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "用户已存在", "错误", JOptionPane.ERROR_MESSAGE);
                }
                UserForm.set(username,password);
            }
        });
        RetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new LoginForm(SignuserForm);
                setVisible(false);
            }
        });
        setVisible(true);
    }
}
